<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhangyh.fun","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言 模块安装  12pip install pandaspip install openpyxl">
<meta property="og:type" content="article">
<meta property="og:title" content="pandas 操作 Excel">
<meta property="og:url" content="https://zhangyh.fun/2021/08/15/pandas%20%E6%93%8D%E4%BD%9CExcel/index.html">
<meta property="og:site_name" content="XiaoPang の Blog">
<meta property="og:description" content="前言 模块安装  12pip install pandaspip install openpyxl">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-15T01:00:00.000Z">
<meta property="article:modified_time" content="2021-08-17T02:20:27.971Z">
<meta property="article:author" content="XiaoPang">
<meta property="article:tag" content="python">
<meta property="article:tag" content="pandas">
<meta property="article:tag" content="excel">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://zhangyh.fun/2021/08/15/pandas%20%E6%93%8D%E4%BD%9CExcel/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>pandas 操作 Excel | XiaoPang の Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?54b35e78bac318b678b24fbb33049203";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


</head>

<body itemscope itemtype="http://schema.org/WebPage">

  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">XiaoPang の Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
    <script src="https://v1.hitokoto.cn/?c=f&encode=js&select=%23hitokoto" defer></script>

      <p class="site-subtitle" itemprop="description" id="hitokoto"><span id="hitokoto_text">记录生活中的点点滴滴</span></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">11</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">34</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhangyh.fun/2021/08/15/pandas%20%E6%93%8D%E4%BD%9CExcel/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="XiaoPang">
      <meta itemprop="description" content="少说，多做">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XiaoPang の Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          pandas 操作 Excel
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于：</span>

              <time title="创建时间：2021-08-15 09:00:00" itemprop="dateCreated datePublished" datetime="2021-08-15T09:00:00+08:00">2021-08-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于：</span>
                <time title="修改时间：2021-08-17 10:20:27" itemprop="dateModified" datetime="2021-08-17T10:20:27+08:00">2021-08-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <br>
            <span class="post-meta-item" title="本文字数">
            <span class="post-meta-item-icon">
            <i class="fa fa-file-word"></i>
            </span>
           <span class="post-meta-item-text">本文字数：</span>
           <span title="post.wordcount">
                1.1k 字
            </span>
            </span>
            <span class="post-meta-item" title="阅读时长">
            <span class="post-meta-item-icon">
            <i class="fa fa-clock"></i>
            </span>
          <span class="post-meta-item-text">阅读时长：</span>
           <span title="post.min2read">
                5 分钟
            </span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><ul>
<li>模块安装</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install pandas</span><br><span class="line">pip install openpyxl</span><br></pre></td></tr></table></figure>
<span id="more"></span>
<h3 id="1-Excel-数据读取"><a href="#1-Excel-数据读取" class="headerlink" title="1. Excel 数据读取"></a>1. <code>Excel</code> 数据读取</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建对象</span></span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd  </span><br><span class="line">pf = pd.read_excel(<span class="string">&quot;amap_poicode.xlsx&quot;</span>, sheet_name=<span class="number">2</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">def read_excel(  </span></span><br><span class="line"><span class="string"> io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=False, dtype: DtypeArg | None = None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=None, mangle_dupe_cols=True, storage_options: StorageOptions = None,):  </span></span><br><span class="line"><span class="string">	pass  </span></span><br><span class="line"><span class="string"># 参数解释：  </span></span><br><span class="line"><span class="string">1. io  </span></span><br><span class="line"><span class="string">excel 路径  </span></span><br><span class="line"><span class="string">2. sheet_name  </span></span><br><span class="line"><span class="string">默认为0。返回多表使用sheet_name=[0, 1];sheet_name=None是返回全表  </span></span><br><span class="line"><span class="string">3. header  </span></span><br><span class="line"><span class="string">默认为0。指定作为列名的行，即第一行，列名行以下的数据为操作数据。若数据不包含列名，则设定header=None。  </span></span><br><span class="line"><span class="string">4. skiprows  </span></span><br><span class="line"><span class="string">省略指定行数的数据  </span></span><br><span class="line"><span class="string">5. skipfooter  </span></span><br><span class="line"><span class="string">省略从尾部数的行数据  </span></span><br><span class="line"><span class="string">6. index_col  </span></span><br><span class="line"><span class="string">指定列为索引列，也可以使用u&#x27;string&#x27;  </span></span><br><span class="line"><span class="string">7. names  </span></span><br><span class="line"><span class="string">指定列的名字，传入一个list数据  </span></span><br><span class="line"><span class="string">8. mangle_dupe_cols  </span></span><br><span class="line"><span class="string">默认为True。重命名重复队列。等于False时，队列中有重复队列会报异常  </span></span><br><span class="line"><span class="string">9. nrows  </span></span><br><span class="line"><span class="string">默认None，解析全部。等于int时，要解析的行数。  </span></span><br><span class="line"><span class="string">10. thousands  </span></span><br><span class="line"><span class="string">默认None，千位分隔符，用于将字符串解析成数字。  </span></span><br><span class="line"><span class="string">11. convert_float  </span></span><br><span class="line"><span class="string">默认True，在可能的情况下，是否将float转换为int(即1.0 --&gt; 1)  </span></span><br><span class="line"><span class="string">12. dtype  </span></span><br><span class="line"><span class="string">默认None，即读取数据会按照可能的类型进行输出，  </span></span><br><span class="line"><span class="string">str，即读取返回的数据为str  </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="1-1-查看数据表行数和列数"><a href="#1-1-查看数据表行数和列数" class="headerlink" title="1.1 查看数据表行数和列数"></a>1.1 查看数据表行数和列数</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(pf.shape)</span><br><span class="line"><span class="comment"># (869, 8)</span></span><br></pre></td></tr></table></figure>
<h4 id="1-2-查看列名"><a href="#1-2-查看列名" class="headerlink" title="1.2 查看列名"></a>1.2 查看列名</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(pf.columns)</span><br><span class="line"><span class="comment"># Index([&#x27;序号&#x27;, &#x27;NEW_TYPE&#x27;, &#x27;大类&#x27;, &#x27;中类&#x27;, &#x27;小类&#x27;, &#x27;Big Category&#x27;, &#x27;Mid Category&#x27;, &#x27;Sub Category&#x27;], dtype=&#x27;object&#x27;)</span></span><br></pre></td></tr></table></figure>
<h4 id="1-3-查看部分行数据"><a href="#1-3-查看部分行数据" class="headerlink" title="1.3 查看部分行数据"></a>1.3 查看部分行数据</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1）查看前几行--&gt;head()默认查看前五行</span></span><br><span class="line"><span class="comment"># 查看前三行数据</span></span><br><span class="line"><span class="built_in">print</span>(df.head(<span class="number">3</span>)) </span><br><span class="line"><span class="comment"># 2）查看最后几行--&gt;tail() 默认查看最后五行</span></span><br><span class="line"><span class="comment"># 查看最后三行</span></span><br><span class="line"><span class="built_in">print</span>(df.tail(<span class="number">3</span>))</span><br></pre></td></tr></table></figure>
<h4 id="1-4-添加索引"><a href="#1-4-添加索引" class="headerlink" title="1.4 添加索引"></a>1.4 添加索引</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 默认情况下，会自动创建一列索引</span></span><br><span class="line"><span class="comment"># 设置指定列为索引</span></span><br><span class="line"><span class="comment"># 1）数据表对象重新赋值</span></span><br><span class="line">df = df.set_index(<span class="string">&quot;索引列&quot;</span>)</span><br><span class="line"><span class="comment"># 之后对df 操作</span></span><br><span class="line"><span class="comment"># 2）直接在当前数据表对象修改设置</span></span><br><span class="line">df.set_index(<span class="string">&quot;索引列&quot;</span>, inplace=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h4 id="1-5-遍历数据"><a href="#1-5-遍历数据" class="headerlink" title="1.5 遍历数据"></a>1.5 遍历数据</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># len(df) 行数</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(df)):</span><br><span class="line">    one_list = []</span><br><span class="line">    <span class="comment"># df.columns，表示列名</span></span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> df.columns:</span><br><span class="line">        one_list.append(df[j][i])</span><br><span class="line">    <span class="built_in">print</span>(one_list)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">dict</span>(<span class="built_in">zip</span>(df.columns, one_list)))</span><br><span class="line"><span class="built_in">print</span>(df.columns)</span><br><span class="line"></span><br><span class="line"><span class="comment">####</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">......</span></span><br><span class="line"><span class="string">[&#x27;867&#x27;, &#x27;991400&#x27;, &#x27;通行设施&#x27;, &#x27;临街院门&#x27;, &#x27;临街院门&#x27;, &#x27;Pass Facilities&#x27;, &#x27;Gate of Street House&#x27;, &#x27;Gate of Street House&#x27;]</span></span><br><span class="line"><span class="string">&#123;&#x27;序号&#x27;: &#x27;867&#x27;, &#x27;NEW_TYPE&#x27;: &#x27;991400&#x27;, &#x27;大类&#x27;: &#x27;通行设施&#x27;, &#x27;中类&#x27;: &#x27;临街院门&#x27;, &#x27;小类&#x27;: &#x27;临街院门&#x27;, &#x27;Big Category&#x27;: &#x27;Pass Facilities&#x27;, &#x27;Mid Category&#x27;: &#x27;Gate of Street House&#x27;, &#x27;Sub Category&#x27;: &#x27;Gate of Street House&#x27;&#125;</span></span><br><span class="line"><span class="string">[&#x27;868&#x27;, &#x27;991401&#x27;, &#x27;通行设施&#x27;, &#x27;临街院门&#x27;, &#x27;临街院正门&#x27;, &#x27;Pass Facilities&#x27;, &#x27;Gate of Street House&#x27;, &#x27;Main Entrance of Street House Gate&#x27;]</span></span><br><span class="line"><span class="string">&#123;&#x27;序号&#x27;: &#x27;868&#x27;, &#x27;NEW_TYPE&#x27;: &#x27;991401&#x27;, &#x27;大类&#x27;: &#x27;通行设施&#x27;, &#x27;中类&#x27;: &#x27;临街院门&#x27;, &#x27;小类&#x27;: &#x27;临街院正门&#x27;, &#x27;Big Category&#x27;: &#x27;Pass Facilities&#x27;, &#x27;Mid Category&#x27;: &#x27;Gate of Street House&#x27;, &#x27;Sub Category&#x27;: &#x27;Main Entrance of Street House Gate&#x27;&#125;</span></span><br><span class="line"><span class="string">[&#x27;869&#x27;, &#x27;991500&#x27;, &#x27;通行设施&#x27;, &#x27;虚拟门&#x27;, &#x27;虚拟门&#x27;, &#x27;Pass Facilities&#x27;, &#x27;Virtual Gate&#x27;, &#x27;Virtual Gate&#x27;]</span></span><br><span class="line"><span class="string">&#123;&#x27;序号&#x27;: &#x27;869&#x27;, &#x27;NEW_TYPE&#x27;: &#x27;991500&#x27;, &#x27;大类&#x27;: &#x27;通行设施&#x27;, &#x27;中类&#x27;: &#x27;虚拟门&#x27;, &#x27;小类&#x27;: &#x27;虚拟门&#x27;, &#x27;Big Category&#x27;: &#x27;Pass Facilities&#x27;, &#x27;Mid Category&#x27;: &#x27;Virtual Gate&#x27;, &#x27;Sub Category&#x27;: &#x27;Virtual Gate&#x27;&#125;</span></span><br><span class="line"><span class="string">Index([&#x27;序号&#x27;, &#x27;NEW_TYPE&#x27;, &#x27;大类&#x27;, &#x27;中类&#x27;, &#x27;小类&#x27;, &#x27;Big Category&#x27;, &#x27;Mid Category&#x27;,</span></span><br><span class="line"><span class="string">       &#x27;Sub Category&#x27;],</span></span><br><span class="line"><span class="string">      dtype=&#x27;object&#x27;)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="2-生成指定格式的Excel"><a href="#2-生成指定格式的Excel" class="headerlink" title="2. 生成指定格式的Excel"></a>2. 生成指定格式的<code>Excel</code></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"> 三行三列数据</span></span><br><span class="line"><span class="string"> A  B   C</span></span><br><span class="line"><span class="string"> 1  10  100</span></span><br><span class="line"><span class="string"> 2  20  200</span></span><br><span class="line"><span class="string"> 3  30  300</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">index_list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">s1 = pd.Series([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], index=index_list, name=<span class="string">&quot;A&quot;</span>)</span><br><span class="line">s2 = pd.Series([<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>], index=index_list, name=<span class="string">&quot;B&quot;</span>)</span><br><span class="line">s3 = pd.Series([<span class="number">100</span>, <span class="number">200</span>, <span class="number">300</span>], index=index_list, name=<span class="string">&quot;C&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 序列表示列</span></span><br><span class="line">df = pd.DataFrame(&#123;s1.name: s1, s2.name: s2, s3.name: s3&#125;)</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">   A   B    C</span></span><br><span class="line"><span class="string">1  1  10  100</span></span><br><span class="line"><span class="string">2  2  20  200</span></span><br><span class="line"><span class="string">3  3  30  300</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 序列表示行</span></span><br><span class="line">df = pd.DataFrame([s1, s2, s3])</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">     1    2    3</span></span><br><span class="line"><span class="string">A    1    2    3</span></span><br><span class="line"><span class="string">B   10   20   30</span></span><br><span class="line"><span class="string">C  100  200  300</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>


    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>XiaoPang
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zhangyh.fun/2021/08/15/pandas%20%E6%93%8D%E4%BD%9CExcel/" title="pandas 操作 Excel">https://zhangyh.fun/2021/08/15/pandas 操作Excel/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://zhangyh.fun/2021/08/15/pandas%20%E6%93%8D%E4%BD%9CExcel/"  aria-label="复制成功！"></i></span>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
        });
    });
</script>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
              <a href="/tags/pandas/" rel="tag"><i class="fa fa-tag"></i> pandas</a>
              <a href="/tags/excel/" rel="tag"><i class="fa fa-tag"></i> excel</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/13/Python%20%E4%B8%BB%E5%8A%A8%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%E5%92%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8/" rel="prev" title="Python 主动抛出异常、自定义异常">
      <i class="fa fa-chevron-left"></i> Python 主动抛出异常、自定义异常
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/19/541.%20%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%20II/" rel="next" title="541. 反转字符串 II">
      541. 反转字符串 II <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    

  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-Excel-%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96"><span class="nav-number">2.</span> <span class="nav-text">1. Excel 数据读取</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E8%A1%A8%E8%A1%8C%E6%95%B0%E5%92%8C%E5%88%97%E6%95%B0"><span class="nav-number">2.1.</span> <span class="nav-text">1.1 查看数据表行数和列数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-%E6%9F%A5%E7%9C%8B%E5%88%97%E5%90%8D"><span class="nav-number">2.2.</span> <span class="nav-text">1.2 查看列名</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-%E6%9F%A5%E7%9C%8B%E9%83%A8%E5%88%86%E8%A1%8C%E6%95%B0%E6%8D%AE"><span class="nav-number">2.3.</span> <span class="nav-text">1.3 查看部分行数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-%E6%B7%BB%E5%8A%A0%E7%B4%A2%E5%BC%95"><span class="nav-number">2.4.</span> <span class="nav-text">1.4 添加索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-5-%E9%81%8D%E5%8E%86%E6%95%B0%E6%8D%AE"><span class="nav-number">2.5.</span> <span class="nav-text">1.5 遍历数据</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%94%9F%E6%88%90%E6%8C%87%E5%AE%9A%E6%A0%BC%E5%BC%8F%E7%9A%84Excel"><span class="nav-number">3.</span> <span class="nav-text">2. 生成指定格式的Excel</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="XiaoPang"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">XiaoPang</p>
  <div class="site-description" itemprop="description">少说，多做</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhang123-123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhang123-123" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/1741290057@qq.com" title="E-Mail → 1741290057@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


<!-- CloudCalendar -->
<div class="widget-wrap" style="width: 90%;margin-left: auto;margin-right: auto; opacity: 0.97;">
	<div class="widget" id="CloudCalendar"></div>
</div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoPang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
    <script type="text/javascript" color="0,0,0" opacity='0.5' zIndex="-2" count="200" src="/js/my_js/canvas-nest.min.js"></script>
    

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/my_js/clicklove.js"></script>

<!-- calendar widget -->

    <script src="/lib/calendar/calendar.min.js"></script>
    <script src="/lib/calendar/languages.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#CloudCalendar').aCalendar('zh-CN',
            $.extend(
                '', {
                    single:true,
                    root:'/calendar/'
                }
            )
        );
    });
    </script>


</html>
